﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UMC.Web.UI;
using UMC.Web;
using UMC.Wiki.Entities;
using System.Collections;

namespace UMC.Wiki.Activities
{



    class SubjectContentActivity : WebActivity
    {



        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var Url = this.AsyncDialog("Id", g =>
            {
                if (request.IsApp == false)
                {
                    response.Redirect(request.Model, "News");
                    return this.DialogValue("News");
                }
                var optls = new Web.UISheetDialog();
                optls.Title = "新建图文"; ;
                optls.Put("新建文档", "Markdown");
                optls.Put(new UIClick("Paste", new UIClick(request.Model, "CaseCMS")) { Text = "抓取网址图文" });

                return optls;
            });
            switch (Url)
            {
                case "News":
                    response.Redirect(request.Model, "News");
                    break;
                case "Markdown":
                    response.Redirect(request.Model, "News", new WebMeta("Type", "Markdown"), true);
                    break;
            }
            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {
                this.Prompt("请先登录", false);
                this.Context.Send("Login", true);

            }

            var sId = UMC.Data.Utility.UInt64Parse(Url, 0);


            var sub = sId > 0 ? DataFactory.Instance().Subject(sId) : new Subject { Id = sId };
            var ms = request.SendValues ?? request.Arguments;

            if (ms.ContainsKey("limit") == false)
            {
                if (sub.project_id.HasValue)
                {
                    var member = DataFactory.Instance().ProjectMember(sub.project_id.Value, user.Id.Value);
                    if (member != null)
                    {
                        switch (member.AuthType)
                        {
                            case WebAuthType.Admin:
                            case WebAuthType.User:
                                break;
                            default:
                                this.Prompt("您未有编辑此图文的权限");
                                break;
                        }

                    }
                    else
                    {
                        this.Prompt("您未有编辑此图文的权限");
                    }
                }

                var con = DataFactory.Instance().Content(sub.Id.Value) ?? new SubjectContent();
                if (String.Equals("markdown", con.ContentType, StringComparison.CurrentCultureIgnoreCase))
                {
                    if (request.IsApp)
                    {
                        switch (this.AsyncDialog("Type", "MD"))
                        {
                            case "MD":
                                this.Context.Send("Markdown", new WebMeta().Put("submit", new UIClick(request, "Type", "Value")), true);
                                break;
                        }

                        var connect = this.AsyncDialog("Content", r =>
                        {
                            response.Redirect(new WebMeta().Put("Content", con.Content ?? String.Empty, "title", sub.Title));
                        });
                        con.Id = sub.Id;
                        con.ContentType = "markdown";
                        con.Content = connect;
                        var cells = Data.Markdown.Transform(connect);
                        var dlist = new ArrayList();
                        foreach (var d in cells)
                        {
                            dlist.Add(new WebMeta().Put("type", d.Type).Put("value", d.Data).Put("format", d.Formats).Put("style", d.Style).GetDictionary());
                        }

                        con.DataJSON = SubjectSubmitActivity.Save(dlist.ToArray(), sub.project_id ?? 0, request.Url);

                        DataFactory.Instance().Put(con);
                        this.Context.Send("Subject.Submit", new WebMeta().Put("timespan", Utility.TimeSpan()).Put("id", con.Id), true);


                    }
                    else
                    {

                        this.Context.Send("Markdown", new WebMeta().Put("Id", sId), true);
                    }
                }
                else
                {

                    if (request.IsApp)
                    {
                        this.Context.Send(new UISectionBuilder(request.Model, request.Command, new UMC.Web.WebMeta().Put("Id", sub.Id))
                            .CloseEvent("Subject.Save")
                                .Builder().Put("IsEditer", true), true);
                    }
                    else
                    {

                        this.Context.Send("Markdown", new WebMeta().Put("Id", sId), true);
                    }


                }

            }

            var Next = this.AsyncDialog("Next", "none");
            var ui = UISection.Create();
            var title = new UITitle("图文编辑器");
            ui.Title = title;
            title.Right(new UIEventText().Icon('\uf0c7').Click(UIClick.Click(new UIClick(request.Model, "Save", "Id", sId.ToString(), "Next", Next))));


            var con2 = DataFactory.Instance().Content(sub.Id.Value) ?? new SubjectContent();
            var celss = UMC.Data.JSON.Deserializes<WebMeta>(String.IsNullOrEmpty(con2.DataJSON) ? "[]" : con2.DataJSON) ?? new UMC.Web.WebMeta[] { };
            foreach (var pom in celss)
            {
                switch (pom["_CetypellName"] ?? pom["type"])
                {
                    case "CMSImage":
                    
                        pom.Put("style", new UIStyle().Padding(0, 10));
                        break;
                }
            }

            if (celss.Length == 0)
            {
                var ed = ui.NewSection();
                ed.DisableSeparatorLine();
                ed.IsEditer = true;
                ed.AddCells(new UMC.Web.WebMeta().Put("type", "CMSText").Put("value", new UMC.Web.WebMeta().Put("text", "新建文档")));
            }
            else
            {
                var ed = ui.NewSection();
                ed.IsEditer = true;
                ed.AddCells(celss);
                ed.DisableSeparatorLine();

            }
            var style = new UIStyle();
            var footer = new UIHeader();
            ui.UIFooter = footer;//
            footer.Desc(new UMC.Web.WebMeta("icon", "\uf004", "desc", "知识录，录入您知识财富"), "{icon}\n{desc}", style);

            style.Height(350).Color(0xf0f0f0).AlignCenter().BgColor(0xf8f8f8).Name("border", "none");//.BorderColor
            style.Name("icon").Font("wdk").Size(40);


            response.Redirect(ui);
        }

    }
}